package com.gaojiawei.multiThread13_ScheduleThreadPoolExecutor;

import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * Schedule thread pool use demo
 */
public class L01_ScheduleThreadPoolExecutorDemo {

    public static void main(String[] args) {

        //1.create a schedule thread
        ScheduledThreadPoolExecutor scheduledThreadPoolExecutor = new ScheduledThreadPoolExecutor(5, new ThreadFactory() {
            @Override
            public Thread newThread(Runnable r) {
                Thread thread = new Thread(r);
                return thread;
            }
        }, new ThreadPoolExecutor.AbortPolicy());

        // no different with ThreadPoolExecutor.execute
        scheduledThreadPoolExecutor.execute(()->{
            System.out.println("use execute");
        });

        //schedule time execute
        scheduledThreadPoolExecutor.schedule(()->{
            System.out.println("user schedule");
        },2, TimeUnit.SECONDS);

        //scheduleAtfixedRate
        scheduledThreadPoolExecutor.scheduleAtFixedRate(()->{
            System.out.println("user scheduleAtFixedRate ");
        },2,3,TimeUnit.SECONDS);

        //scheuleWithFixedDelay
        scheduledThreadPoolExecutor.scheduleWithFixedDelay(()->{
            System.out.println("user scheduleWithFixedDelay");
        },2,3,TimeUnit.SECONDS);

    }
}
